//
//  JYJBigCollectionViewCellTWo.m
//  HappyListen
//
//  Created by 冀永金 on 16/8/31.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJBigCollectionViewCellTWo.h"
#import "BaseCollectionView.h"
#import "JYJTAMacro.h"
#import "JYJBigSortCollectionViewCell.h"
#import "JYJNetWorkParser.h"
#import "JYJSortModel.h"
#import "JYJVCOfSortContent.h"
#import "MJRefresh.h"
@interface JYJBigCollectionViewCellTWo ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *sortCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray<JYJSortModel *> *marrOfModel;
@property (nonatomic, strong) UIImageView *imageOfAnimation;
@end
@implementation JYJBigCollectionViewCellTWo
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
        [self createAnimation];
        [self handleData];
    }
    return self;
}
- (void)handleData {
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/channellist.jhtml" andBody:@"json=%7B%7D" andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
       
        self.marrOfModel = [NSMutableArray array];
        NSArray *dataArray =result[@"data"];
        for (NSDictionary *dic in dataArray) {
            JYJSortModel *model = [JYJSortModel modelWithDic:dic];
            [self.marrOfModel addObject:model];
        }
        [self.sortCollectionView reloadData];
        self.imageOfAnimation.hidden = YES;
    } andFailureBlock:^(NSError *error) {
        
    }];

}
- (void)createSubviews {
    _layout = [[UICollectionViewFlowLayout alloc]init];
    _layout.minimumLineSpacing = 40;
    _layout.sectionInset = UIEdgeInsetsMake(20, 30, 20, 30);
    _sortCollectionView = [[BaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    [self.contentView addSubview:_sortCollectionView];
    _sortCollectionView.delegate = self;
    _sortCollectionView.dataSource = self;
    [_sortCollectionView registerClass:[JYJBigSortCollectionViewCell class] forCellWithReuseIdentifier:@"SortCell"];
    // 下拉刷新动画
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        NSString *name = [NSString stringWithFormat:@"list_load_%d",i];
        UIImage *image = [UIImage imageNamed:name];
        [marr addObject:image];
    }
     [header setImages:marr forState:MJRefreshStateIdle];
      [header setImages:marr forState:MJRefreshStatePulling];
      [header setImages:marr forState: MJRefreshStateRefreshing];
    self.sortCollectionView.mj_header = header;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
}
- (void)loadNewData {
    [self handleData];
    [self.sortCollectionView.mj_header endRefreshing];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 120) / 3, (SCREEN_WIDTH - 120) / 3);
    _sortCollectionView.frame = self.bounds;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.marrOfModel.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJBigSortCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SortCell" forIndexPath:indexPath];
    JYJSortModel *model = self.marrOfModel[indexPath.item];
    [cell setSortModel:model];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJVCOfSortContent *sortContent = [[JYJVCOfSortContent alloc]init];
     JYJSortModel *model = self.marrOfModel[indexPath.item];
    sortContent.sortModel = model;
    self.handleAction(sortContent);
}
- (void)createAnimation {
    self.imageOfAnimation = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"main_load_0"]];
    self.imageOfAnimation.frame = CGRectMake(SCREEN_WIDTH / 2 - 30, 150, 80, 200);
    [self addSubview:_imageOfAnimation];
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < 5; i++) {
        NSString *imageName = [NSString stringWithFormat:@"main_load_%d",i];
        UIImage *image = [UIImage imageNamed:imageName];
        [marr addObject:image];
    }
    _imageOfAnimation.animationDuration = 1;
    _imageOfAnimation.animationImages  = marr;
    _imageOfAnimation.animationRepeatCount = 0;
    [ _imageOfAnimation startAnimating];
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
